package com.mew.chess.core.game.lh.game.core;

import com.mew.chess.core.game.lh.game.common.LhCard;
import com.zebra.common.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 游戏逻辑
 */
@Data
@ToString
public class LhGame {

    public int loong;
    public int hu;
    public int remove;

    //0  和， 1  龙 ，2  虎
    public int res;

    public List<Integer> cards = new ArrayList<>();

    public int cardSize;

    public void initDice(int c){

        if (c==0){
            doDice();
        }else if (c==1){
            doDice();
            if (res!=c){

                int i = 0;
                while (res!=c && i<50){
                    cards.add(loong);
                    cards.add(hu);
                    cards.add(remove);
                    cardSize+=3;
                    doDice();
                    System.out.println("--");
                }

            }
        }

    }

    private void doDice() {
        if (cardSize<3){
            //8套牌 发 2/3
            //洗牌
            cards.clear();
            for (int i = 0; i < 8; i++) {
                for (int j = 1; j < 53; j++) {
                    cards.add(j);
                }
            }

            Collections.shuffle(cards);
        }


        loong = cards.get(0);
        cards.remove(0);

        hu = cards.get(0);
        cards.remove(0);

        remove = cards.get(0);
        cards.remove(0);

        res = LhCard.card(loong).num>LhCard.card(hu).num?1:(LhCard.card(loong).num<LhCard.card(hu).num?2:0);

        cardSize = cards.size();
    }

    /**
     * @param betType
     * @return
     */
    public double payment(int betType){

        if (res == betType){
            if (res == 0){
                return 9;
            }else {
                return 2;
            }
        }else if (res == 0){
            return 0.5;
        }

        return 0;

    }

    public void write(ByteBuf b){

        ByteBufUtils.writeInt(b,loong);
        ByteBufUtils.writeInt(b,hu);
        ByteBufUtils.writeInt(b,res);
        ByteBufUtils.writeInt(b,cardSize);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {

            LhGame lhGame = new LhGame();
            lhGame.initDice(1);
            System.out.println(lhGame.res+"----"+lhGame.cardSize+"---"+lhGame);
        }

    }

}
